using System;
using System.Collections.Generic;
using System.Text;

namespace BizTalk.ESB.Extensions.Helpers
{
    [Serializable]
    public class FileNameBuilder
    {
        #region Members

        private String _DateFormat;
        public String DateFormat
        {
            get { return _DateFormat; }
            set { _DateFormat = value; }
        }

        private List<String> _FileNameParts;
        public List<String> FileNameParts
        {
            get { return _FileNameParts; }
            set { _FileNameParts = value; }
        }

        private String _FileExtension;
        public String FileExtension
        {
            get { return _FileExtension; }
            set { _FileExtension = value; }
        }

        private String _DateString;
        public String DateString
        {
            get { return _DateString; }
            set { _DateString = value; }
        }
	
        #endregion

        public FileNameBuilder(String dateFormat, String dateString, String fileExtension)
        {
            if (String.IsNullOrEmpty(fileExtension))
            {
                throw new ArgumentException("Must provide a file extension.  Value cannot be null.");
            }
            else
            {
                this.FileExtension = fileExtension;
            }
            if (String.IsNullOrEmpty(dateFormat))
            {
                this.DateFormat = String.Empty;
            }
            else
            {
                this.DateFormat = dateFormat;
            }
            if (String.IsNullOrEmpty(dateString))
            {
                throw new ArgumentNullException("A valid datetime string must be provided.");
            }
            else
            {
                this.DateString = dateString;
            }
            this.FileNameParts = new List<string>();
        }

        public void AddPart(String part)
        {
            if (String.IsNullOrEmpty(part))
            {
                throw new ArgumentNullException("Part cannot be null or empty.");
            }
            else
            {
                this.FileNameParts.Add(part);
            }
        }

        public String BuildFileName()
        {
            StringBuilder fileName = new StringBuilder();
            if (this.FileNameParts != null &&
                String.IsNullOrEmpty(this.FileExtension) == false &&
                String.IsNullOrEmpty(this.DateFormat) == false)
            {
                if (FileNameParts.Count > 0)
                {
                    foreach (String part in this.FileNameParts)
                    {
                        if (String.IsNullOrEmpty(part) == false)
                        {
                            if (part == "DATE")
                            {
                                fileName.Append(this.GetDate());
                            }
                            else if (part == "GUID")
                            {
                                fileName.Append(this.GenerateGuid());
                            }
                            else
                            {
                                fileName.Append(part);
                            }
                        }
                    }
                    fileName.Append(".");
                    fileName.Append(this.FileExtension);
                    return fileName.ToString();
                }
                else
                {
                    throw new ArgumentException("No valid file name parts were provided.");
                }
            }
            else
            {
                throw new ArgumentNullException("One more more required fields is null.");
            }
        }

        private String GenerateGuid()
        {
            return Guid.NewGuid().ToString();
        }

        private String GetDate()
        {
            if (String.IsNullOrEmpty(this.DateString))
            {
                this.DateString = System.DateTime.Now.ToString();
            }
            if (String.IsNullOrEmpty(this.DateFormat) == false)
            {
                DateTime dt;
                DateTime.TryParse(this.DateString, out dt);
                return dt.ToString(this.DateFormat);
            }
            else
            {
                throw new ArgumentNullException("DateTime Format string cannot be null.");
            }
        }
    }
}
